home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / emacssrc.zip / EMACSSRC.TAR / emacs-19.17 / src / getloadavg.c < prev    next >
C/C++ Source or Header  |  1993-10-07  |  21KB  |  856 lines

  1. /* Get the system load averages.
  2.    Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93
  3.        Free Software Foundation, Inc.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /* Compile-time symbols that this file uses:
  20.  
  21.    FIXUP_KERNEL_SYMBOL_ADDR()    Adjust address in returned struct nlist.
  22.    KERNEL_FILE            Pathname of the kernel to nlist.
  23.    LDAV_CVT()            Scale the load average from the kernel.
  24.                 Returns a double.
  25.    LDAV_SYMBOL            Name of kernel symbol giving load average.
  26.    LOAD_AVE_TYPE        Type of the load average array in the kernel.
  27.                 Must be defined unless one of
  28.                 apollo, DGUX, NeXT, or UMAX is defined;
  29.                 otherwise, no load average is available.
  30.    NLIST_STRUCT            Include nlist.h, not a.out.h, and
  31.                 the nlist n_name element is a pointer,
  32.                 not an array.
  33.    NLIST_NAME_UNION        struct nlist has an n_un member, not n_name.
  34.    LINUX_LDAV_FILE        [LINUX]: Name of file containing load averages.
  35.  
  36.    Specific system predefines this file uses, aside from setting
  37.    default values if not emacs:
  38.  
  39.    apollo
  40.    BSD                Real BSD, not just BSD-like.
  41.    DGUX
  42.    eunice            UNIX emulator under VMS.
  43.    hpux
  44.    NeXT
  45.    sgi
  46.    sequent            Sequent Dynix 3.x.x (BSD)
  47.    _SEQUENT_            Sequent DYNIX/ptx 1.x.x (SYSV)
  48.    sony_news                    NEWS-OS (works at least for 4.1C)
  49.    UMAX
  50.    UMAX4_3
  51.    VMS
  52.    LINUX            Linux: assumes /proc filesystem mounted.
  53.                    Support from Michael K. Johnson.
  54.  
  55.    In addition, to avoid nesting many #ifdefs, we internally set
  56.    LDAV_DONE to indicate that the load average has been computed.
  57.  
  58.    We also #define LDAV_PRIVILEGED if a program will require
  59.    special installation to be able to call getloadavg.  */
  60.  
  61. #include <sys/types.h>
  62.  
  63. /* Both the Emacs and non-Emacs sections want this.  Some
  64.    configuration files' definitions for the LOAD_AVE_CVT macro (like
  65.    sparc.h's) use macros like FSCALE, defined here.  */
  66. #ifdef unix
  67. #include <sys/param.h>
  68. #endif
  69.  
  70.  
  71. #ifdef HAVE_CONFIG_H
  72. #include "config.h"
  73. #endif
  74.  
  75. #ifdef emacs
  76. #ifdef STDC_HEADERS
  77. #include <io.h>
  78. #endif
  79. #include "getloadavg_p.h"
  80. #endif
  81.  
  82. /* The existing Emacs configuration files define a macro called
  83.    LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
  84.    returns the load average multiplied by 100.  What we actually want
  85.    is a macro called LDAV_CVT, which returns the load average as an
  86.    unmultiplied double.
  87.  
  88.    For backwards compatibility, we'll define LDAV_CVT in terms of
  89.    LOAD_AVE_CVT, but future machine config files should just define
  90.    LDAV_CVT directly.  */
  91.  
  92. #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
  93. #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
  94. #endif
  95.  
  96. #if !defined (BSD) && defined (ultrix)
  97. /* Ultrix behaves like BSD on Vaxen.  */
  98. #define BSD
  99. #endif
  100.  
  101. #ifdef NeXT
  102. /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
  103.    conflicts with the definition understood in this file, that this
  104.    really is BSD. */
  105. #undef BSD
  106.  
  107. /* NeXT defines FSCALE in <sys/param.h>.  However, we take FSCALE being
  108.    defined to mean that the nlist method should be used, which is not true.  */
  109. #undef FSCALE
  110. #endif
  111.  
  112. /* Set values that are different from the defaults, which are
  113.    set a little farther down with #ifndef.  */
  114.  
  115.  
  116. /* Some shorthands.  */
  117.  
  118. #if defined (HPUX) && !defined (hpux)
  119. #define hpux
  120. #endif
  121.  
  122. #if defined(hp300) && !defined(hpux)
  123. #define MORE_BSD
  124. #endif
  125.  
  126. #if defined(ultrix) && defined(mips)
  127. #define decstation
  128. #endif
  129.  
  130. #if defined(sun) && defined(SVR4)
  131. #define SUNOS_5
  132. #endif
  133.  
  134. #if defined (__osf__) && defined (__alpha__)
  135. #define OSF_ALPHA
  136. #endif
  137.  
  138. #if defined (__osf__) && (defined (mips) || defined (__mips__))
  139. #define OSF_MIPS
  140. #include <sys/table.h>
  141. #endif
  142.  
  143. /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
  144.    default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>.  Combine
  145.    that with a couple of other things and we'll have a unique match.  */
  146. #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
  147. #define tek4300            /* Define by emacs, but not by other users.  */
  148. #endif
  149.  
  150.  
  151. /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars.  */
  152. #ifndef LOAD_AVE_TYPE
  153.  
  154. #ifdef MORE_BSD
  155. #define LOAD_AVE_TYPE long
  156. #endif
  157.  
  158. #ifdef sun
  159. #define LOAD_AVE_TYPE long
  160. #endif
  161.  
  162. #ifdef decstation
  163. #define LOAD_AVE_TYPE long
  164. #endif
  165.  
  166. #ifdef _SEQUENT_
  167. #define LOAD_AVE_TYPE long
  168. #endif
  169.  
  170. #ifdef sgi
  171. #define LOAD_AVE_TYPE long
  172. #endif
  173.  
  174. #ifdef SVR4
  175. #define LOAD_AVE_TYPE long
  176. #endif
  177.  
  178. #ifdef sony_news
  179. #define LOAD_AVE_TYPE long
  180. #endif
  181.  
  182. #ifdef sequent
  183. #define LOAD_AVE_TYPE long
  184. #endif
  185.  
  186. #ifdef OSF_ALPHA
  187. #define LOAD_AVE_TYPE long
  188. #endif
  189.  
  190. #if defined (ardent) && defined (titan)
  191. #define LOAD_AVE_TYPE long
  192. #endif
  193.  
  194. #ifdef tek4300
  195. #define LOAD_AVE_TYPE long
  196. #endif
  197.  
  198. #endif /* No LOAD_AVE_TYPE.  */
  199.  
  200. #ifndef    FSCALE
  201.  
  202. /* SunOS and some others define FSCALE in sys/param.h.  */
  203.  
  204. #ifdef MORE_BSD
  205. #define FSCALE 2048.0
  206. #endif
  207.  
  208. #if defined(MIPS) || defined(SVR4) || defined(decstation)
  209. #define FSCALE 256
  210. #endif
  211.  
  212. #if defined (sgi) || defined (sequent)
  213. #define    FSCALE 1000.0
  214. #endif
  215.  
  216. #if defined (ardent) && defined (titan)
  217. #define FSCALE 65536.0
  218. #endif
  219.  
  220. #ifdef tek4300
  221. #define FSCALE 100.0
  222. #endif
  223.  
  224. #ifdef WINDOWSNT
  225. #define FSCALE 100.0
  226. #endif
  227.  
  228. #endif    /* Not FSCALE.  */
  229.  
  230. #if !defined (LDAV_CVT) && defined (FSCALE)
  231. #define    LDAV_CVT(n) (((double) (n)) / FSCALE)
  232. #endif
  233.  
  234. /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters.  */
  235. #ifndef NLIST_STRUCT
  236.  
  237. #ifdef MORE_BSD
  238. #define NLIST_STRUCT
  239. #endif
  240.  
  241. #ifdef sun
  242. #define NLIST_STRUCT
  243. #endif
  244.  
  245. #ifdef decstation
  246. #define NLIST_STRUCT
  247. #endif
  248.  
  249. #ifdef hpux
  250. #define NLIST_STRUCT
  251. #endif
  252.  
  253. #if defined (_SEQUENT_) || defined (sequent)
  254. #define NLIST_STRUCT
  255. #endif
  256.  
  257. #ifdef sgi
  258. #define NLIST_STRUCT
  259. #endif
  260.  
  261. #ifdef SVR4
  262. #define NLIST_STRUCT
  263. #endif
  264.  
  265. #ifdef sony_news
  266. #define NLIST_STRUCT
  267. #endif
  268.  
  269. #ifdef OSF_ALPHA
  270. #define NLIST_STRUCT
  271. #endif
  272.  
  273. #if defined (ardent) && defined (titan)
  274. #define NLIST_STRUCT
  275. #endif
  276.  
  277. #ifdef tex4300
  278. #define NLIST_STRUCT
  279. #endif
  280.  
  281. #ifdef butterfly
  282. #define NLIST_STRUCT
  283. #endif
  284.  
  285. #endif /* defined (NLIST_STRUCT) */
  286.  
  287.  
  288. #if defined(sgi) || (defined(mips) && !defined(BSD))
  289. #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
  290. #endif
  291.  
  292.  
  293. #if !defined (KERNEL_FILE) && defined (sequent)
  294. #define KERNEL_FILE "/dynix"
  295. #endif
  296.  
  297. #if !defined (KERNEL_FILE) && defined (hpux)
  298. #define KERNEL_FILE "/hp-ux"
  299. #endif
  300.  
  301. #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
  302. #define KERNEL_FILE "/unix"
  303. #endif
  304.  
  305.  
  306. #if !defined (LDAV_SYMBOL) && defined (alliant)
  307. #define LDAV_SYMBOL "_Loadavg"
  308. #endif
  309.  
  310. #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
  311. #define LDAV_SYMBOL "avenrun"
  312. #endif
  313.  
  314. #ifdef HAVE_UNISTD_H
  315. #include <unistd.h>
  316. #endif
  317.  
  318. #include <stdio.h>
  319. #include <errno.h>
  320.  
  321. #ifndef errno
  322. extern int errno;
  323. #endif
  324.  
  325. /* LOAD_AVE_TYPE should only get defined if we're going to use the
  326.    nlist method.  */
  327. #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
  328. #define LOAD_AVE_TYPE double
  329. #endif
  330.  
  331. #ifdef LOAD_AVE_TYPE
  332.  
  333. #ifndef VMS
  334. #ifndef NLIST_STRUCT
  335. #ifndef WINDOWSNT
  336. #include <a.out.h>
  337. #endif /* !WINDOWSNT */
  338. #else /* NLIST_STRUCT */
  339. #include <nlist.h>
  340. #endif /* NLIST_STRUCT */
  341.  
  342. #ifdef SUNOS_5
  343. #include <fcntl.h>
  344. #include <kvm.h>
  345. #endif
  346.  
  347. #ifndef KERNEL_FILE
  348. #define KERNEL_FILE "/vmunix"
  349. #endif /* KERNEL_FILE */
  350.  
  351. #ifndef LDAV_SYMBOL
  352. #define LDAV_SYMBOL "_avenrun"
  353. #endif /* LDAV_SYMBOL */
  354.  
  355. #else /* VMS */
  356.  
  357. #ifndef eunice
  358. #include <iodef.h>
  359. #include <descrip.h>
  360. #else /* eunice */
  361. #include <vms/iodef.h>
  362. #endif /* eunice */
  363. #endif /* VMS */
  364.  
  365. #ifndef LDAV_CVT
  366. #define LDAV_CVT(n) ((double) (n))
  367. #endif /* !LDAV_CVT */
  368.  
  369. #endif /* LOAD_AVE_TYPE */
  370.  
  371. #ifdef NeXT
  372. #ifdef HAVE_MACH_MACH_H
  373. #include <mach/mach.h>
  374. #else
  375. #include <mach.h>
  376. #endif
  377. #endif /* NeXT */
  378.  
  379. #ifdef sgi
  380. #include <sys/sysmp.h>
  381. #endif /* sgi */
  382.  
  383. #ifdef UMAX
  384. #include <stdio.h>
  385. #include <signal.h>
  386. #include <sys/time.h>
  387. #include <sys/wait.h>
  388. #include <sys/syscall.h>
  389.  
  390. #ifdef UMAX_43
  391. #include <machine/cpu.h>
  392. #include <inq_stats/statistics.h>
  393. #include <inq_stats/sysstats.h>
  394. #include <inq_stats/cpustats.h>
  395. #include <inq_stats/procstats.h>
  396. #else /* Not UMAX_43.  */
  397. #include <sys/sysdefs.h>
  398. #include <sys/statistics.h>
  399. #include <sys/sysstats.h>
  400. #include <sys/cpudefs.h>
  401. #include <sys/cpustats.h>
  402. #include <sys/procstats.h>
  403. #endif /* Not UMAX_43.  */
  404. #endif /* UMAX */
  405.  
  406. #ifdef DGUX
  407. #include <sys/dg_sys_info.h>
  408. #endif
  409.  
  410. #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
  411. #include <fcntl.h>
  412. #else
  413. #ifndef WINDOWSNT
  414. #include <sys/file.h>
  415. #endif /* !WINDOWSNT */
  416. #endif
  417.  
  418. /* Avoid static vars inside a function since in HPUX they dump as pure.  */
  419.  
  420. #ifdef NeXT
  421. static processor_set_t default_set;
  422. static int getloadavg_initialized;
  423. #endif /* NeXT */
  424.  
  425. #ifdef UMAX
  426. static unsigned int cpus = 0;
  427. static unsigned int samples;
  428. #endif /* UMAX */
  429.  
  430. #ifdef DGUX
  431. static struct dg_sys_info_load_info load_info;    /* what-a-mouthful! */
  432. #endif /* DGUX */
  433.  
  434. #ifdef LOAD_AVE_TYPE
  435. /* File descriptor open to /dev/kmem or VMS load ave driver.  */
  436. static int channel;
  437. /* Nonzero iff channel is valid.  */
  438. static int getloadavg_initialized;
  439. /* Offset in kmem to seek to read load average, or 0 means invalid.  */
  440. static long offset;
  441.  
  442. #if !defined(VMS) && !defined(sgi) && !defined(WINDOWSNT)
  443. static struct nlist nl[2];
  444. #endif /* Not VMS or sgi */
  445.  
  446. #ifdef SUNOS_5
  447. static kvm_t *kd;
  448. #endif /* SUNOS_5 */
  449.  
  450. #endif /* LOAD_AVE_TYPE */
  451.  
  452. /* Put the 1 minute, 5 minute and 15 minute load averages
  453.    into the first NELEM elements of LOADAVG.
  454.    Return the number written (never more than 3, but may be less than NELEM),
  455.    or -1 if an error occurred.  */
  456.  
  457. int
  458. getloadavg (loadavg, nelem)
  459.      double loadavg[];
  460.      int nelem;
  461. {
  462.   int elem = 0;            /* Return value.  */
  463.  
  464. #ifdef NO_GET_LOAD_AVG
  465. #define LDAV_DONE
  466.   /* Set errno to zero to indicate that there was no particular error;
  467.      this function just can't work at all on this system.  */
  468.   errno = 0;
  469.   elem = -1;
  470. #endif
  471.  
  472. #if !defined (LDAV_DONE) && defined (LINUX)
  473. #define LDAV_DONE
  474. #undef LOAD_AVE_TYPE
  475.  
  476. #ifndef LINUX_LDAV_FILE
  477. #define LINUX_LDAV_FILE "/proc/loadavg"
  478. #endif
  479.  
  480.   char ldavgbuf[40];
  481.   double load_ave[3];
  482.   int fd, count;
  483.  
  484.   fd = open (LINUX_LDAV_FILE, O_RDONLY);
  485.   if (fd == -1)
  486.     return -1;
  487.   count = read (fd, ldavgbuf, 40);
  488.   (void) close (fd);
  489.   if (count <= 0)
  490.     return -1;
  491.  
  492.   count = sscanf (ldavgbuf, "%lf %lf %lf",
  493.           &load_ave[0], &load_ave[1], &load_ave[2]);
  494.   if (count < 1)
  495.     return -1;
  496.  
  497.   for (elem = 0; elem < nelem && elem < count; elem++)
  498.     loadavg[elem] = load_ave[elem];
  499.  
  500.   return elem;
  501.  
  502. #endif /* LINUX */
  503.  
  504. #if !defined (LDAV_DONE) && defined (NeXT)
  505. #define LDAV_DONE
  506.   /* The NeXT code was adapted from iscreen 3.2.  */
  507.  
  508.   host_t host;
  509.   struct processor_set_basic_info info;
  510.   unsigned info_count;
  511.  
  512.   /* We only know how to get the 1-minute average for this system,
  513.      so even if the caller asks for more than 1, we only return 1.  */
  514.  
  515.   if (!getloadavg_initialized)
  516.     {
  517.       if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
  518.     getloadavg_initialized = 1;
  519.     }
  520.  
  521.   if (getloadavg_initialized)
  522.     {
  523.       info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
  524.       if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
  525.                  (processor_set_info_t) &info, &info_count)
  526.       != KERN_SUCCESS)
  527.     getloadavg_initialized = 0;
  528.       else
  529.     {
  530.       if (nelem > 0)
  531.         loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
  532.     }
  533.     }
  534.  
  535.   if (!getloadavg_initialized)
  536.     return -1;
  537. #endif /* NeXT */
  538.  
  539. #if !defined (LDAV_DONE) && defined (UMAX)
  540. #define LDAV_DONE
  541. /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
  542.    have a /dev/kmem.  Information about the workings of the running kernel
  543.    can be gathered with inq_stats system calls.
  544.    We only know how to get the 1-minute average for this system.  */
  545.  
  546.   struct proc_summary proc_sum_data;
  547.   struct stat_descr proc_info;
  548.   double load;
  549.   register unsigned int i, j;
  550.  
  551.   if (cpus == 0)
  552.     {
  553.       register unsigned int c, i;
  554.       struct cpu_config conf;
  555.       struct stat_descr desc;
  556.  
  557.       desc.sd_next = 0;
  558.       desc.sd_subsys = SUBSYS_CPU;
  559.       desc.sd_type = CPUTYPE_CONFIG;
  560.       desc.sd_addr = (char *) &conf;
  561.       desc.sd_size = sizeof conf;
  562.  
  563.       if (inq_stats (1, &desc))
  564.     return -1;
  565.  
  566.       c = 0;
  567.       for (i = 0; i < conf.config_maxclass; ++i)
  568.     {
  569.       struct class_stats stats;
  570.       bzero ((char *) &stats, sizeof stats);
  571.  
  572.       desc.sd_type = CPUTYPE_CLASS;
  573.       desc.sd_objid = i;
  574.       desc.sd_addr = (char *) &stats;
  575.       desc.sd_size = sizeof stats;
  576.  
  577.       if (inq_stats (1, &desc))
  578.         return -1;
  579.  
  580.       c += stats.class_numcpus;
  581.     }
  582.       cpus = c;
  583.       samples = cpus < 2 ? 3 : (2 * cpus / 3);
  584.     }
  585.  
  586.   proc_info.sd_next = 0;
  587.   proc_info.sd_subsys = SUBSYS_PROC;
  588.   proc_info.sd_type = PROCTYPE_SUMMARY;
  589.   proc_info.sd_addr = (char *) &proc_sum_data;
  590.   proc_info.sd_size = sizeof (struct proc_summary);
  591.   proc_info.sd_sizeused = 0;
  592.  
  593.   if (inq_stats (1, &proc_info) != 0)
  594.     return -1;
  595.  
  596.   load = proc_sum_data.ps_nrunnable;
  597.   j = 0;
  598.   for (i = samples - 1; i > 0; --i)
  599.     {
  600.       load += proc_sum_data.ps_nrun[j];
  601.       if (j++ == PS_NRUNSIZE)
  602.     j = 0;
  603.     }
  604.  
  605.   if (nelem > 0)
  606.     loadavg[elem++] = load / samples / cpus;
  607. #endif /* UMAX */
  608.  
  609. #if !defined (LDAV_DONE) && defined (DGUX)
  610. #define LDAV_DONE
  611.   /* This call can return -1 for an error, but with good args
  612.      it's not supposed to fail.  The first argument is for no
  613.      apparent reason of type `long int *'.  */
  614.   dg_sys_info ((long int *) &load_info,
  615.            DG_SYS_INFO_LOAD_INFO_TYPE,
  616.            DG_SYS_INFO_LOAD_VERSION_0);
  617.  
  618.   if (nelem > 0)
  619.     loadavg[elem++] = load_info.one_minute;
  620.   if (nelem > 1)
  621.     loadavg[elem++] = load_info.five_minute;
  622.   if (nelem > 2)
  623.     loadavg[elem++] = load_info.fifteen_minute;
  624. #endif /* DGUX */
  625.  
  626. #if !defined (LDAV_DONE) && defined (apollo)
  627. #define LDAV_DONE
  628. /* Apollo code from lisch@mentorg.com (Ray Lischner).
  629.  
  630.    This system call is not documented.  The load average is obtained as
  631.    three long integers, for the load average over the past minute,
  632.    five minutes, and fifteen minutes.  Each value is a scaled integer,
  633.    with 16 bits of integer part and 16 bits of fraction part.
  634.  
  635.    I'm not sure which operating system first supported this system call,
  636.    but I know that SR10.2 supports it.  */
  637.  
  638.   extern void proc1_$get_loadav ();
  639.   unsigned long load_ave[3];
  640.  
  641.   proc1_$get_loadav (load_ave);
  642.  
  643.   if (nelem > 0)
  644.     loadavg[elem++] = load_ave[0] / 65536.0;
  645.   if (nelem > 1)
  646.     loadavg[elem++] = load_ave[1] / 65536.0;
  647.   if (nelem > 2)
  648.     loadavg[elem++] = load_ave[2] / 65536.0;
  649. #endif /* apollo */
  650.  
  651. #if !defined (LDAV_DONE) && defined (OSF_MIPS)
  652. #define LDAV_DONE
  653.  
  654.   struct tbl_loadavg load_ave;
  655.   table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  656.   loadavg[elem++]
  657.     = (load_ave.tl_lscale == 0
  658.        ? load_ave.tl_avenrun.d[0]
  659.        : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
  660. #endif    /* OSF_MIPS */
  661.  
  662. #if !defined (LDAV_DONE) && defined (VMS)
  663.   /* VMS specific code -- read from the Load Ave driver.  */
  664.  
  665.   LOAD_AVE_TYPE load_ave[3];
  666.   static int getloadavg_initialized = 0;
  667. #ifdef eunice
  668.   struct
  669.   {
  670.     int dsc$w_length;
  671.     char *dsc$a_pointer;
  672.   } descriptor;
  673. #endif
  674.  
  675.   /* Ensure that there is a channel open to the load ave device.  */
  676.   if (!getloadavg_initialized)
  677.     {
  678.       /* Attempt to open the channel.  */
  679. #ifdef eunice
  680.       descriptor.dsc$w_length = 18;
  681.       descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
  682. #else
  683.       $DESCRIPTOR (descriptor, "LAV0:");
  684. #endif
  685.       if (sys$assign (&descriptor, &channel, 0, 0) & 1)
  686.     getloadavg_initialized = 1;
  687.     }
  688.  
  689.   /* Read the load average vector.  */
  690.   if (getloadavg_initialized
  691.       && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
  692.              load_ave, 12, 0, 0, 0, 0) & 1))
  693.     {
  694.       sys$dassgn (channel);
  695.       getloadavg_initialized = 0;
  696.     }
  697.  
  698.   if (!getloadavg_initialized)
  699.     return -1;
  700. #endif /* VMS */
  701.  
  702. #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
  703.  
  704.   /* UNIX-specific code -- read the average from /dev/kmem.  */
  705.  
  706. #define LDAV_PRIVILEGED        /* This code requires special installation.  */
  707.  
  708.   LOAD_AVE_TYPE load_ave[3];
  709.  
  710.   /* Get the address of LDAV_SYMBOL.  */
  711.   if (offset == 0)
  712.     {
  713. #ifndef sgi
  714. #ifndef NLIST_STRUCT
  715. #ifndef WINDOWSNT
  716.       strcpy (nl[0].n_name, LDAV_SYMBOL);
  717.       strcpy (nl[1].n_name, "");
  718. #endif /* !WINDOWSNT */
  719. #else /* NLIST_STRUCT */
  720. #ifdef NLIST_NAME_UNION
  721.       nl[0].n_un.n_name = LDAV_SYMBOL;
  722.       nl[1].n_un.n_name = 0;
  723. #else /* not NLIST_NAME_UNION */
  724.       nl[0].n_name = LDAV_SYMBOL;
  725.       nl[1].n_name = 0;
  726. #endif /* not NLIST_NAME_UNION */
  727. #endif /* NLIST_STRUCT */
  728.  
  729. #ifndef SUNOS_5
  730. #ifndef WINDOWSNT
  731.       if (nlist (KERNEL_FILE, nl) >= 0)
  732.     /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i.  */
  733.     {
  734. #ifdef FIXUP_KERNEL_SYMBOL_ADDR
  735.       FIXUP_KERNEL_SYMBOL_ADDR (nl);
  736. #endif
  737.       offset = nl[0].n_value;
  738.     }
  739. #endif /* !WINDOWSNT */
  740. #endif  /* !SUNOS_5 */
  741. #else /* sgi */
  742.       int ldav_off;
  743.  
  744.       ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
  745.       if (ldav_off != -1)
  746.     offset = (long) ldav_off & 0x7fffffff;
  747. #endif /* sgi */
  748.     }
  749.  
  750.   /* Make sure we have /dev/kmem open.  */
  751.   if (!getloadavg_initialized)
  752.     {
  753. #ifndef SUNOS_5
  754.       channel = open ("/dev/kmem", 0);
  755.       if (channel >= 0)
  756.     getloadavg_initialized = 1;
  757. #else /* SUNOS_5 */
  758.       /* We pass 0 for the kernel, corefile, and swapfile names
  759.      to use the currently running kernel.  */
  760.       kd = kvm_open (0, 0, 0, O_RDONLY, 0);
  761.       if (kd != 0) 
  762.     {
  763.       /* nlist the currently running kernel.  */
  764.       kvm_nlist (kd, nl);
  765.       offset = nl[0].n_value;
  766.       getloadavg_initialized = 1;
  767.     }
  768. #endif /* SUNOS_5 */
  769.     }
  770.  
  771.   /* If we can, get the load average values.  */
  772.   if (offset && getloadavg_initialized)
  773.     {
  774.       /* Try to read the load.  */
  775. #ifndef SUNOS_5
  776.       if (lseek (channel, offset, 0) == -1L
  777.       || read (channel, (char *) load_ave, sizeof (load_ave))
  778.       != sizeof (load_ave))
  779.     {
  780.       close (channel);
  781.       getloadavg_initialized = 0;
  782.     }
  783. #else  /* SUNOS_5 */
  784.       if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
  785.       != sizeof (load_ave))
  786.         {
  787.           kvm_close (kd);
  788.           getloadavg_initialized = 0;
  789.     }
  790. #endif /* SUNOS_5 */
  791.     }
  792.  
  793.   if (offset == 0 || !getloadavg_initialized)
  794.     return -1;
  795. #endif /* LOAD_AVE_TYPE and not VMS */
  796.  
  797. #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS.  */
  798.   if (nelem > 0)
  799.     loadavg[elem++] = LDAV_CVT (load_ave[0]);
  800.   if (nelem > 1)
  801.     loadavg[elem++] = LDAV_CVT (load_ave[1]);
  802.   if (nelem > 2)
  803.     loadavg[elem++] = LDAV_CVT (load_ave[2]);
  804.  
  805. #define LDAV_DONE
  806. #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
  807.  
  808. #ifdef LDAV_DONE
  809.   return elem;
  810. #else
  811.   /* Set errno to zero to indicate that there was no particular error;
  812.      this function just can't work at all on this system.  */
  813.   errno = 0;
  814.   return -1;
  815. #endif
  816. }
  817.  
  818. #ifdef TEST
  819. void
  820. main (argc, argv)
  821.      int argc;
  822.      char **argv;
  823. {
  824.   int naptime = 0;
  825.  
  826.   if (argc > 1)
  827.     naptime = atoi (argv[1]);
  828.  
  829.   if (naptime == 0)
  830.     naptime = 5;
  831.  
  832.   while (1)
  833.     {
  834.       double avg[3];
  835.       int loads;
  836.  
  837.       errno = 0;        /* Don't be misled if it doesn't set errno.  */
  838.       loads = getloadavg (avg, 3);
  839.       if (loads == -1)
  840.     {
  841.       perror ("Error getting load average");
  842.       exit (1);
  843.     }
  844.       if (loads > 0)
  845.     printf ("1-minute: %f  ", avg[0]);
  846.       if (loads > 1)
  847.     printf ("5-minute: %f  ", avg[1]);
  848.       if (loads > 2)
  849.     printf ("15-minute: %f  ", avg[2]);
  850.       if (loads > 0)
  851.     putchar ('\n');
  852.       sleep (naptime);
  853.     }
  854. }
  855. #endif /* TEST */
  856.